//
//  FMGravityBehaviorViewController.m
//  物理仿真
//
//  Created by Annie on 2020/5/23.
//  Copyright © 2020 Annie. All rights reserved.
//

#import "FMGravityBehaviorViewController.h"


#import "FMEggBehaviorView.h"
#import "FRTimerManager.h"

@interface FMGravityBehaviorViewController ()<FMEggBehaviorViewDelegete,UICollisionBehaviorDelegate>
@property (nonatomic, strong) UIButton *btn;

@property (nonatomic, strong) UIView *view1;
// 增加碰撞检测
@property (nonatomic, strong) UIDynamicAnimator * animator;
@property (nonatomic, strong) UIGravityBehavior *gravity;
@property (nonatomic, strong) UICollisionBehavior *collision;



@property (nonatomic, strong) UIView *view2;
@property (nonatomic, strong) UIDynamicAnimator * animator1;
@property (nonatomic, strong) UICollisionBehavior *collision1;
@property (nonatomic, strong) UIPushBehavior *pushBehavior;

@end

@implementation FMGravityBehaviorViewController


- (void)count{
    
    int x = arc4random()%(int)(self.view.bounds.size.width - 100) + 50;
    
    CGPoint center = CGPointMake((CGFloat)x, 100);
    
    [self addColorViewWithCenter:center];
}

- (void)start{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(count) name:kFRTimerCountNotification object:nil];
}
- (void)pasuse{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kFRTimerCountNotification object:nil];
    
}

- (UIDynamicAnimator *)animator{
    if (!_animator) {
        UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view1];
        _animator = animator;
    }
    return _animator;
}

- (UIGravityBehavior *)gravity{
    if (!_gravity) {
        UIGravityBehavior *gravity = [UIGravityBehavior new];
        gravity.gravityDirection = CGVectorMake(0, 0.35);
        _gravity = gravity;
    }
    return _gravity;
}
- (UICollisionBehavior *)collision{
    if (!_collision) {
        UICollisionBehavior *collision = [UICollisionBehavior new];
        collision.collisionMode = UICollisionBehaviorModeBoundaries;
        collision.collisionDelegate = self;
        
        CGSize size = self.view.bounds.size;
        [collision addBoundaryWithIdentifier:@"bottom" fromPoint:CGPointMake(-100, size.height) toPoint:CGPointMake(size.width, size.height)];

        _collision = collision;
        
    }
    return _collision;
}

- (UIDynamicAnimator *)animator1{
    if (!_animator1) {
        UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view2];
        _animator1 = animator;
    }
    return _animator1;
}

- (UICollisionBehavior *)collision1{
    if (!_collision1) {
        UICollisionBehavior *collision = [UICollisionBehavior new];
        collision.collisionMode = UICollisionBehaviorModeBoundaries;
        collision.collisionDelegate = self;
        
        CGSize size = self.view.bounds.size;
        [collision addBoundaryWithIdentifier:@"left" fromPoint:CGPointMake(-100, 0) toPoint:CGPointMake(-100, size.height)];

        _collision1 = collision;
        
    }
    return _collision1;
}



- (UIPushBehavior *)pushBehavior{
    if (!_pushBehavior) {
        UIPushBehavior *pushBehavior = [[UIPushBehavior alloc] initWithItems:@[] mode:UIPushBehaviorModeContinuous];
        pushBehavior.pushDirection = CGVectorMake(-1, 0);
        
        _pushBehavior = pushBehavior;
    }
    return _pushBehavior;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UIView *view2 = [UIView new];
    [self.view addSubview:view2];
    self.view2 = view2;
    
    self.view2.frame = self.view.bounds;

    
    
    UIView *view1 = [UIView new];
    [self.view addSubview:view1];
    self.view1 = view1;
    
    self.view1.frame = self.view.bounds;
    

    
    
    [self.view addSubview:self.btn];
    self.btn.frame = CGRectMake(0, 100, 100, 40);
    
}

- (UIButton *)btn{
    if (!_btn) {
        UIButton *btn = [UIButton new];
        btn.backgroundColor = [UIColor lightGrayColor];
        [btn setTitle:@"开始" forState:UIControlStateNormal];
        [btn setTitle:@"结束" forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        _btn = btn;
    }
    return _btn;
}


- (void)btnDidClicked:(UIButton *)sender{
    
    if (sender.selected) {
        [self pasuse];
    }else{
        [self start];
    }
    sender.selected = !sender.selected;
}





#pragma mark - FMEggBehaviorViewDelegete
- (void)catchedWithEggBehaviorView:(FMEggBehaviorView *)colorView{
    if (colorView.fr_status == kFREggStatusNormal) {
        colorView.fr_status = kFREggStatusCatched;
    }
}


- (void)addColorViewWithCenter:(CGPoint)center{
    
        
    FMEggBehaviorView *colorView = [FMEggBehaviorView new];
    colorView.delegate = self;
    [self.view addSubview:colorView];
    

    [self.view1 addSubview:colorView];
    colorView.frame = CGRectMake(0, 0, 100, 100);
    colorView.center = center;
    
    
    [self.gravity addItem:colorView];
    [self.collision addItem:colorView];
    
    
    if (self.animator.behaviors.count == 0) {
        [self.animator addBehavior:self.gravity];
        [self.animator addBehavior:self.collision];
    }
}








- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p{
    
}
- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2{
    
}

// 和边界产生碰撞
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p{
    FMEggBehaviorView *colorView = (FMEggBehaviorView *)item;
    NSString *identifierStr = (NSString *)identifier;
    if ([identifierStr isEqualToString:@"left"]) {
        
        
        [self.collision1 removeItem:colorView];
        [self.pushBehavior addItem:colorView];
        [colorView removeFromSuperview];
        NSLog(@"😄over");
    }else if ([identifierStr isEqualToString:@"bottom"]) {
        
        if (colorView.fr_status != kFREggStatusCatched) {
            colorView.fr_status = kFREggStatusNotCatched;
            
            [self.gravity removeItem:colorView];
            [self.collision removeItem:colorView];
            __block UIView *itemView = colorView;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [itemView removeFromSuperview];
            });
            return;
        }
        NSLog(@"...");
        [self.gravity removeItem:colorView];
        [self.collision removeItem:colorView];
        
        [colorView removeFromSuperview];
        [self.view2 addSubview:colorView];
        
        
        
        if (self.animator1.behaviors.count == 0) {
            [self.animator1 addBehavior:self.collision1];
            [self.animator1 addBehavior:self.pushBehavior];
        }

        [self.collision1 addItem:colorView];
        [self.pushBehavior addItem:colorView];
        
        CGFloat scale = self.view2.subviews.count/10.0;
        // 控制横向推力
        self.pushBehavior.pushDirection = CGVectorMake(-1 - scale * 50, 0);
        
    }
    
}





- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier{
    
}




@end
